<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript">

		function edit(a){

			
// 			alert(ids)
// 			alert(name)
// 			alert(addr)
			
			// this 就是当前点击的元素，input
			// $ 转换为jQuery对象
			// .parent()获取父元素
			//          td -> tr
			// 父元素tr
			var $tr = $(a).parent().parent() 
			
			
			var $allTds = $tr.find("td");

			var id = $allTds.eq(0).text();
			var name = $allTds.eq(1).text();
			var address = $allTds.eq(2).text();
			
		
			
			
			
		//data:$('#myform').serialize(),
				$.ajax({
					url:'user/edit.do',
					type:'post',
					datatype:'json',  //返回的数据结果是JSON数据
					data:{
						'id':id,
						'name':name,
						'address':address
					},
					success:function(data){
						
						// 数据成功返回！
						//    前端  --> 后端  --> 前端   流程走完，没有异常
						
						alert(data.ret);
						
						if (data.ret == 0) {
							
							alert("更新成功！")
						} else {

							alert(data.msg)
						}
						
					},
					error:function(data) {
						// 错误：不是正常返回
						// alert(data.responseText);
						
						// 弹窗，提示用户，当前服务器
					}
				});
		}
	function save(){
		//data:$('#myform').serialize(),
				$.ajax({
					url:'user/save.do',
					type:'post',
					datatype:'html',
					data:'ids=3',
					success:function(data){
						var a = JSON.parse(data);
						var b = eval('('+data+')');
						alert(a.suc);
						alert(b.suc);
					},
					error:function(data){
						alert(data.responseText);
					}
				});
		}
		
		function show(id){
			$.ajax({
						url:'user/detail/'+id+'.do',
						type:'get',
						datatype:'json',
						//data:'ids=3',
						success:function(data){
							//alert(data.ret);
							
							if (data.ret == 0) {
								var str = "";

								str += "id:" + data.data.id;
								str += "name:" + data.data.name;
								str += "price:" + data.data.price;
								alert(str)
							} else {
								alert("获取失败")
								
							}
						},
						error:function(data){
							alert(data.responseText);
						}
					});
		//alert(id);
		//location.href="${pageContext.request.contextPath}/user/detail/"+id;
		//window.open ("${pageContext.request.contextPath}/user/detail/"+id);
		//showModalDialog("${pageContext.request.contextPath}/user/detail.do/"+id,"aa","dialogWidth:400px;dialogHeight:300px;dialogLeft:200px;dialogTop:150px;center:yes;help:yes;resizable:yes;status:yes"); 
		}
		function reg(){
			location.href="${pageContext.request.contextPath}/user/findUserTPF/indexab.do";
			//location.href="${pageContext.request.contextPath}/user/reg/liquan-32.do";
		}
	</script>
  </head>
  
  <body>
  
  	<!-- form 表单提交数据， 
  			后台的接口地址， action 属性
  					这里是： /user/findUserByReq.do
  					
  			请求方式：
  					method = "post"		
  					
  			
  			/springmvc/user/findUserByReq.do
  				/ 开头，代表的服务器根目录， 绝对路径
  			
  			
  			user/findUserByReq.do	 相对路径
  				是相对于  index.jsp 这个文件来说的
  				
  			http://localhost:8080/springmvc/
  			
  			http://localhost:8080/springmvc/user/findUserByReq.do
  	 -->
    <form id="myform" action = "user/findUserByReq.do" method = "post" >
    	<label>username:</label><input type = "text" id="username" name = "username" value=""/>
    	<label>password:</label><input type = "text" id = "password" name = "password" value=""/>
    	<input type = "submit" value="submit">
    	
    	
    	<input type = "button" value="正则" onclick="reg()"/>
    	<input type = "button" value="save" onclick="save()"/>
    	<input type = "button" value="通配符" onclick="reg()"/>
    	<table border = "1" style="width:500px">
    		<tr><td>序号</td><td>名称</td><td>地址</td><td>操作</td></tr>
    		<tr><td>1</td><td>aa</td><td>bb</td><td><input type = "button" value="查看" onclick="show(1)"/></td></tr>
    		<tr><td>2</td><td>aa</td><td>bb</td><td><input type = "button" value="查看" onclick="show(2)"/></td></tr>
    		<tr><td>3</td><td>aa</td><td>bb</td><td><input id="editid" type = "button" value="编辑" onclick="edit(this)" /></td></tr>
    	</table>
    	
    </form>
  </body>
</html>
